//
//  RoomDeviceSelectListView.m
//  wAudioShare
//
//  Created by Anson on 14-11-16.
//  Copyright (c) 2014年 赵帅. All rights reserved.
//

#import "RoomDeviceSelectListView.h"
#import "RoomItemModel.h"
#import "AutomationCell.h"
#import "GlobalService.h"
#import "ChangeInfoDialog.h"

@interface RoomDeviceSelectListView () <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) UITableView *tbView;
@property (strong, nonatomic) NSArray *deviceList;
@property (strong, nonatomic) NSMutableArray *deviceSelectedList;

@end

CGFloat HeightOfBottomButton = 50.0;

@implementation RoomDeviceSelectListView

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        CGRect rectTableView = frame;
        rectTableView.size.height -= HeightOfBottomButton;
        _tbView = [[UITableView alloc] initWithFrame:rectTableView style:UITableViewStylePlain];
        _tbView.dataSource = self;
        _tbView.delegate = self;
        _tbView.allowsMultipleSelection = YES;
        [self addSubview:_tbView];
        
        [self addBottomConfirmButtonToViewWithY:rectTableView.size.height];
    }
    return self;
}

//- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
//    self = [super initWithFrame:frame style:style];
//    if (self) {
//        
//        self.dataSource = self;
//        self.delegate = self;
//        
//        self.allowsMultipleSelection = YES;
//        
//    }
//    return self;
//}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)addBottomConfirmButtonToViewWithY:(CGFloat)y {
    
    CGRect rect = CGRectMake(0, y, self.bounds.size.width, HeightOfBottomButton);
    UIView *container = [[UIView alloc] initWithFrame:rect];
    container.backgroundColor = [UIColor whiteColor];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"确定" forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor redColor];
    btn.frame = CGRectMake(0, 5, 200, HeightOfBottomButton - 10);
    btn.center = CGPointMake(rect.size.width/2, rect.size.height/2);
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 0.7;
    [btn addTarget:self action:@selector(onConfirmBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [container addSubview:btn];
    
    [self addSubview:container];
}

- (void)setDeviceDataSource:(NSArray *)devlist {
    self.deviceList = devlist;
    [_tbView reloadData];
}

- (NSMutableArray *)deviceSelectedList {
    if (!_deviceSelectedList) {
        _deviceSelectedList = [[NSMutableArray alloc] initWithCapacity:10];
    }
    return _deviceSelectedList;
}

- (void)onConfirmBtnClicked {
    
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(roomDeviceSelectListView:confirmSelectDev:)]) {
        [self.customDelegate roomDeviceSelectListView:self confirmSelectDev:_deviceSelectedList];
    }
    
    
}

#pragma mark - UITableView Delegate & DataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.deviceList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
    NSUInteger row=[indexPath row];
    static NSString *AutomationCellIdentifier = @"AutomationCellIdentifier";
    AutomationCell *cell = (AutomationCell*)[tableView dequeueReusableCellWithIdentifier:AutomationCellIdentifier];
    if (cell==nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"AutomationCell" owner:self options:nil]lastObject];
        [cell initView];
        //cell.adelegate = self;
       
        UIImageView *checkImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchosen2"] highlightedImage:[UIImage imageNamed:@"chosen2"]];
        checkImgView.bounds = CGRectMake(0, 0, 30, 30);
        checkImgView.tag = 1001;
        checkImgView.center = CGPointMake( self.bounds.size.width - 60  , 70/2);
        //checkImgView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        [cell.contentView addSubview:checkImgView];
    }
    NSMutableDictionary *rowData = [self.deviceList objectAtIndex:row];
    
    //    NSMutableDictionary *muDic = [rowData mutableCopy];
    NSString *strUuid = [[GlobalService sharedInstance].curOperationDeviceUUID stringByReplacingOccurrencesOfString:@":" withString:@""];
    NSString *zidStr = rowData[@"ZID"];
    NSString *appStr = [NSString stringWithFormat:@"%@%@",strUuid,zidStr];
    [rowData setObject:appStr forKey:@"uuid"];
    [cell setCellData:rowData];
    cell.curtainCtrlView.hidden = YES;
    cell.switcher.hidden = YES;
    cell.slider.hidden = YES;
    
    
    UIImageView *checkImgView = (UIImageView *)[cell.contentView viewWithTag:1001];
    checkImgView.highlighted = [rowData[@"checked"] boolValue];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DLog(@"");
    NSMutableDictionary *rowData = self.deviceList[indexPath.row];
    NSNumber *n = rowData[@"checked"];
    BOOL isChecked = ![n boolValue];
    rowData[@"checked"] = [NSNumber numberWithBool:isChecked];
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UIImageView *checkImgView = (UIImageView *)[cell.contentView viewWithTag:1001];
    checkImgView.highlighted = isChecked;
    
    if (isChecked) [self.deviceSelectedList addObject:rowData];
    else [self.deviceSelectedList removeObject:rowData];
}

//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
//    return 50;
//}
//
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
//    CGRect rect = CGRectMake(0, 0, self.bounds.size.width, 50);
//    UIView *container = [[UIView alloc] initWithFrame:rect];
//    //container.backgroundColor = [UIColor yellowColor];
//    
//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [btn setTitle:@"确定" forState:UIControlStateNormal];
//    btn.backgroundColor = [UIColor redColor];
//    btn.bounds = CGRectMake(0, 0, 200, 40);
//    btn.center = CGPointMake(rect.size.width/2, rect.size.height/2);
//    btn.layer.masksToBounds = YES;
//    btn.layer.cornerRadius = 0.7;
//    [btn addTarget:self action:@selector(onConfirmBtnClicked) forControlEvents:UIControlEventTouchUpInside];
//    [container addSubview:btn];
//    return container;
//}

@end
